﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using RegulatoryPlatform.ScheduleTask;

namespace RegulatoryPlatform
{
    class Program
    {
        static async Task Main(string[] args)
        {
            StdSchedulerFactory factory = new StdSchedulerFactory();
            IScheduler scheduler = await factory.GetScheduler();

            //每天的23:00执行. (目前只需要设置前3位 分别对应:秒 分 时)
            string cronExpression = "10 * * * * ?";

            await AddJob<YL_YPPSXX>(scheduler, "job1", "trigger1", cronExpression);
            await AddJob<YL_ZXFZJL>(scheduler, "job2", "trigger2", cronExpression);
            await AddJob<YL_DZCFXX>(scheduler, "job3", "trigger3", cronExpression);
            await AddJob<TJ_HLWYY_YWL>(scheduler, "job4", "trigger4", cronExpression);
            await AddJob<TJ_HLWYY_YWSR>(scheduler, "job5", "trigger5", cronExpression);

            await scheduler.Start();

            Console.ReadKey();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="scheduler"></param>
        /// <param name="jobName"></param>
        /// <param name="triggerName"></param>
        /// <param name="cronExpression">更多参考 https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/crontrigger.html#format</param>
        /// <param name="action"></param>
        /// <returns></returns>
        private static async Task AddJob<T>(IScheduler scheduler, string jobName, string triggerName, string cronExpression, Action<SimpleScheduleBuilder> action = null) where T : IJob
        {
            IJobDetail job = JobBuilder.Create<T>()
                .WithIdentity(jobName, "group1")
                .Build();
            ITrigger trigger = null;

            TriggerBuilder tb = TriggerBuilder.Create()
                .WithIdentity(triggerName, "group1")
                .StartNow()
                .WithCronSchedule(cronExpression);

            if (action != null)
            {
                tb = tb.WithSimpleSchedule(action);
            }

            trigger = tb.Build();
            await scheduler.ScheduleJob(job, trigger);
        }

    }
}
